import { frontAndBackDay, formatDate } from "@/utils/util";
import { formatData, eOption } from "./rule";
export default {
  data() {
    return {
      Request: {
        startDate: "",
        airlineCode: "",
        from: "",
        to: "",
        cabinCode: "",
        day: 30
      },

      flightNoList: [
        {
          value: "CZ3000",
          label: "CZ3000"
        }
      ],
      cabinCodeList: [],
      data: null,
      originalData: null,
      show: false
    };
  },
  computed: {},
  watch: {},
  methods: {
    //搜索
    async search() {
      let res = await this.getData(); //先请求
      if (res) this.drawEcharts(); //请求之后绘制echarts表格
    },
    getData() {
      //格式化请求的日期
      this.Request.startDate = formatDate(this.Request.startDate);
      //将promise对象返回
      return this.$http
        .post("/api/forecast", this.Request)
        .then(res => {
          if (res.data.code == 601) {
            if(res.data.data.list.length==0){
                this.$Message.info("数据为空"); 
                return;
            }
            this.show = true;
            this.originalData = JSON.parse(JSON.stringify(res.data.data.list));
            //格式化返回结果
            this.data = formatData(
              JSON.parse(JSON.stringify(res.data.data.list))
            );
            return true;
          }
          //请求失败的处理
          this.show = false;
          this.$Notice.error({
            title: "查询失败",
            desc: res.data.message
          });
          return false;
        })
        .catch(err => {
          //请求失败的处理
          this.show = false;
          this.$Notice.error({
            title: "查询失败",
            desc: err
          });
          return false;
        });
    },
    toCenter(day) {    
      const query = {
        airlineCode:day.airlineCode,
        cabinCode:day.cabinCode,
        from:day.depAirport,
        to:day.arrAirport,
        date:day.forecastDate
      }
      this.$router.push({ path: "/decision/center", query: query });
    },
    drawEcharts() {
      let option = eOption(this.originalData);
      let ele = this.$refs.tendency;
      let myChart = this.$echarts.init(ele);
      myChart.setOption(option);
    },
    //时间选择变化
    startDateChange(val){
      if(!this.Request.airlineCode) return;
      let params = {'flightNo':this.Request.airlineCode,'date':val}
      this.getCabinList(params);
    },
    //航班变化
    airlineCodeChange(val){
      if(!this.Request.startDate) return;
      let params = {'flightNo':val,'date':formatDate(this.Request.startDate)}
      this.getCabinList(params);
    },
    //请求舱位数据
    getCabinList(params){
      this.cabinCodeList = [];
      this.$http.post('/api/cabin/list',params)
      .then(res => {
          if(res.data.code=='601'){
            for(let i of res.data.data){
              let cabin = {
                value: i.classCode,
                label: i.classCode
              }
              this.cabinCodeList.push(cabin);
            }
          }
      })
    },
    //翻页查询
    changePage(type) {
      if (type == "next") {
        this.Request.startDate = frontAndBackDay(this.Request.startDate, 30);
      }
      if (type == "prev") {
        this.Request.startDate = frontAndBackDay(this.Request.startDate, -30);
      }
      this.search();
    },
    getSaleDataLastUpdateDate(){
      this.$http.post('/api/config/list')
        .then(res => {
            if(res.data.code=='601'){
              this.Request.startDate = res.data.data[0].saleDataLastUpdateDate;
            }
        })
    }
  },
  components: {},
  created() {
    this.getSaleDataLastUpdateDate();
  }
};
